package com.ququ.syb.transferorder.service;

import com.ququ.syb.common.constant.TransferStatusConstant;
import com.ququ.syb.common.pojo.TransferOrder;
import com.ququ.syb.transferorder.dao.TransferOrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class TransferOrderService {
    @Autowired
    private TransferOrderDao transferOrderDao;
    @Autowired
    private TransferOrderTradenumService transferOrderTradenumService;

    @Cacheable(value = "cache", key = "'transferorder:'+#tradenum", unless = "#result == null")
    public TransferOrder getTransferOrder(String tradenum){
        return transferOrderDao.getTransferOrder(tradenum);
    }


    @Transactional
    @CachePut(value = "cache", key = "'withdraworder:'+#transferOrder.tradenum", unless = "#result == null")
    public TransferOrder save(TransferOrder transferOrder){
        transferOrder.setTradenum(transferOrderTradenumService.getNextTradenum());
        transferOrder.setTransfertime(new Date());
        transferOrder.setStatus(TransferStatusConstant.INIT);
        transferOrderDao.save(transferOrder);
        return transferOrderDao.getTransferOrder(transferOrder.getTradenum());
    }

    @Transactional
    @CachePut(value = "cache", key = "'withdraworder:'+#transferOrder.tradenum", unless = "#result == null")
    public TransferOrder update(TransferOrder transferOrder){
        transferOrderDao.update(transferOrder);
        return transferOrderDao.getTransferOrder(transferOrder.getTradenum());
    }


}
